package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户模块")
/*@Slf4j 注解用于简化日志记录器的创建*/
@Slf4j
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("login")
    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 10)
    public JsonResult login(@Validated @RequestBody UserLoginParam userLoginParam) {
        log.debug("登录功能:userLoginParam={}", userLoginParam);
        UserVO userVO=userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }
    @GetMapping("select")
    @Operation(summary = "查询用户")
    @ApiOperationSupport(order = 20)
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("查询用户:userQuery={}",userQuery);
        List<UserVO> list=userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }
    @PostMapping("save")
    @Operation(summary = "保存")
    @ApiOperationSupport(order = 30)
    public JsonResult saveUser(@Validated  UserSaveParam userSaveParam){
        log.debug("新增用户:userSaveParam={}",userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }
    @PostMapping("update/password/{userId}")
    @Operation(summary = "重置密码")
    @ApiOperationSupport(order = 40)
    public JsonResult resetPassword(@PathVariable Long userId){
        log.debug("重置密码:userId={}",userId);
        userService.resetPassword(userId);
        return JsonResult.ok();
    }
    //注意此时与前端顺序对应
    @PostMapping("update/status/{userId}/{status}")
    @Operation(summary = "更新状态")
    @ApiOperationSupport(order = 50)
    public JsonResult updateStatus(@PathVariable Long userId,@PathVariable Integer status){
        log.debug("更新状态:userId={},status={}",userId,status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }
    @PostMapping("delete/{userId}")
    @Operation(summary = "删除用户")
    @ApiOperationSupport(order = 60)
    public JsonResult deleteUser(@PathVariable Long userId){
        log.debug("删除用户:userId={}",userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }
    @GetMapping("select/audit/{parentId}")
    @Operation(summary = "查询审批人")
    @ApiOperationSupport(order = 70)
    public JsonResult selectAuditUser(@PathVariable Long parentId){
        log.debug("查询审批人:parentId={}",parentId);
        List<UserVO> list=userService.selectAuditUserList(parentId);
        return JsonResult.ok(list);
    }
}
